package com.lupan.mutithred.cp1_SemaphoreAndExchanger;

import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 说明：
 *
 * @author lupan
 * @version 2017/3/30 10:44
 */
public class Service {
    private Semaphore semaphore = new Semaphore(3);
    private ReentrantLock lock = new ReentrantLock();

    public void sayHello() throws InterruptedException {
        lock.lock();

        semaphore.acquire();
        System.out.println(String.format("线程名=%s 准备。。。",Thread.currentThread().getName()));
        System.out.println("begin hello "+ System.currentTimeMillis());
        for (int i = 0; i < 5; i++) {
            System.out.println(String.format("%s打印%s",Thread.currentThread().getName(),i+1));

        }
        System.out.println("end hello " + System.currentTimeMillis());
        semaphore.release();
        System.out.println(String.format("线程名=%s 结束。。。",Thread.currentThread().getName()));

        lock.unlock();

    }
}
